package com.linln.modules.violation.service.impl;

import com.linln.common.utils.UUIDUtil;
import com.linln.common.utils.ViolationHttpUtils;
import com.linln.component.cache.util.RedisUtil;
import com.linln.modules.violation.service.ViolationService;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.violation.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.violation.service.impl
 * @Description: TODO
 * @date Date : 2021年01月09日 9:26
 */
@Service
@Log4j2
public class ViolationServiceImpl implements ViolationService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map<String, Object> queryViolation(String plate, String engineno, String frameno) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        plate = plate.trim();
        engineno = engineno.trim();
        frameno = frameno.trim();
        if (plate.length() != 7) {
            map.put("msg", "车牌号有误");
            return map;
        }

        if (engineno.length() != 6 || !IsItANumber(engineno)) {
            map.put("msg", "发动机号有误");
            return map;
        }

        if (frameno.length() != 6 || !IsItANumber(frameno)) {
            map.put("msg", "VIN有误");
            return map;
        }
        try {
            //判断缓存中是否存在
            if (redisUtil.get(plate + engineno + frameno + "valid") != null) {
                Map<String, Object> result = (Map<String, Object>) redisUtil.get(plate + engineno + frameno);
                if (result != null) {
                    map.put("code", 200);
                    map.put("msg", "获取成功");
                    String uuid = UUIDUtil.randomUUID32();
                    redisUtil.set(uuid, plate + engineno + frameno, 60 * 60 * 24);
                    map.put("data", uuid);
                    return map;
                }
            } else {
                //查询违章信息
                JSONObject jsonObject = ViolationHttpUtils.queryViolation(plate, engineno, frameno);
                //是否查询成功
                if (jsonObject.getInt("status") != 0) {
                    map.put("msg", jsonObject.getString("msg"));
                    System.out.println(jsonObject.getString("msg"));
                } else {
                    net.sf.json.JSONObject resultarr = jsonObject.optJSONObject("result");
                    if (resultarr != null) {
                        String uuid = UUIDUtil.randomUUID32();
                        map.put("code", 200);
                        map.put("msg", "获取成功");
                        redisUtil.set(uuid, plate + engineno + frameno, 60 * 60 * 24);
                        redisUtil.set(plate + engineno + frameno, resultarr, 0);
                        redisUtil.set(plate + engineno + frameno + "valid", 0, 60 * 60 * 24);
                        map.put("data", uuid);
//                    return resultarr;
                    }
                }
            }
        } catch (Exception e) {
            log.info(e.toString());
            map.put("msg", "获取违章信息失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> queryViolationByUUID(String id) {
        Map<String, Object> map = new HashMap<>();
        Object key = redisUtil.get(id);
        map.put("data", redisUtil.get((String) key));
        map.put("code", 200);
        return map;
    }


    //是否是数字和字母
    private static boolean IsItANumber(String str) {
        Pattern pattern = Pattern.compile("^[0-9a-zA-Z]+$");
        return pattern.matcher(str).matches();

    }
}
